package com.spring.tool;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.spring.widget.CustomProgressDialog;
import com.tencent.android.tpush.XGNotifaction;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.android.tpush.XGPushNotifactionCallback;

import java.util.List;

public class App extends Application {
    private static App app;
    //public String url="http://120.237.110.50:8097/AndriodApp/WebService.asmx";
    //public String url="http://10.10.8.249:8072/WebService.asmx";
    public static final String URL = "http://120.237.110.50:8072/WebService.asmx";
    public static final String Public = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB";
    public static final String Private = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKnG1cpiiA/koWnE4w/8nrP2eyJmqy3iTIm6oDe/M+TVDB61QA6Lhh+dMebJziwBPyJbvkH9FHsnTmCfcoaG7PY6wWLNviKbcrvA5MocQ6eldsih2M438e7nzoP7GXlDXZvRCKC6xUJnXZJEe8vg/8xQlHr7bX7MKWffZXf+rZiPAgMBAAECgYAvQbtMhDSfR+JuEHGrh4OdUCRiJPXM5cqsI5hpK/2D2pFvEsBrzAZfUmALf2y17vrl02eX8pv606wLVRaJv5DBXJtIaGPyGLtRYSMrpvydnJhUZRp/o6IecJF9jix4Qn6k1Xn+D5E6sG8BaHv7c9N3Ej3s8aBpoiHcD6rbtR4MgQJBAOFuMUGq8ekWiLZrGveYhbri5JUpBvHOzRnipm5z4zHbLvsgzMWJVo8gF7Y4ZpzmXWAGn+1uagqL3QZk0CKPueECQQDAzKAqCBjNV0IM6NGbgDWhaFhIio0PmzO6x9NWcutEPfqZzWUu7AK5XuF8I/YYTUK88+tIozpUnCLkzk/l3ABvAkEAj0dwOyKhetp9riPJEhF4/d10F3HqHgqoxYkO6yjxACElbSvduredsn5dU83pOlQYiQ3s3AXPlEBtDMFKoIzlQQJACvDRufDhr21fMypXErLw2Cr6r4slPBh7lALY7FPzI9k8ilPMhSAhe3F17DyCL5jay6wv0txoZ/rARCmtAQc+AwJAdyy2YwvUfFTNFJdcCulsB7yjACxqWwrpubqcwjQEDgyjFC1YmOTHGY+lH2HKIqOU0pggWV07oCkH91/4zvHXbQ==";
    public String url = URL;

    //public int isLogin=0;
    public static App getInstance() {
        if (app == null) {
            app = new App();
        }
        return app;
    }

    public static int localVersion = 0;// 本地安装版本

    public static int serverVersion = 2;// 服务器版本  

    public static String downloadDir = "jj/";// 安装目录
    SharedPreferences userInfo;

    private boolean mlogin;

    public boolean isLogin() {
        userInfo = getSharedPreferences(
                "user_info", 0);
        mlogin = userInfo.getBoolean("login", false);
        return mlogin;
    }


    public String getApiUrl() {
        return userInfo.getString("apiUrl", URL);
    }

    public void setApiUrl(String url) {
        this.url = url;
        userInfo.edit().putString("apiUrl", url).commit();
    }

    private CustomProgressDialog mDialog;

    public void showProgressDialog(final Activity activity) {
        mDialog = CustomProgressDialog.show(activity, "加载数据中...", false, null);
        new Handler().postDelayed(new Runnable() {
            public void run() {
               longLength(activity);

            }
        }, 8000);
    }

    public void cancelProgressDialog() {
        if (mDialog != null)
            mDialog.cancel();
    }

    public void longLength(Activity activity) {
        if (mDialog != null)
            mDialog.cancel();
        if(Tools.getAPNType(activity) == -1){
            Toast.makeText(activity, "服务器连接超长", Toast.LENGTH_SHORT).show();
        }

    }



    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        userInfo = getSharedPreferences(
                "user_info", 0);
        mlogin = userInfo.getBoolean("login", false);
        url = getApiUrl();
        try {
            PackageInfo packageInfo = getApplicationContext()
                    .getPackageManager().getPackageInfo(getPackageName(), 0);
            localVersion = packageInfo.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        /*** 
         * 在这里写一个方法用于请求获取服务器端的serverVersion. 
         */


        // 在主进程设置信鸽相关的内容
        if (isMainProcess()) {
            // 为保证弹出通知前一定调用本方法，需要在application的onCreate注册
            // 收到通知时，会调用本回调函数。
            // 相当于这个回调会拦截在信鸽的弹出通知之前被截取
            // 一般上针对需要获取通知内容、标题，设置通知点击的跳转逻辑等等
            XGPushManager
                    .setNotifactionCallback(new XGPushNotifactionCallback() {

                        @Override
                        public void handleNotify(XGNotifaction xGNotifaction) {
                            Log.i("test", "处理信鸽通知：" + xGNotifaction);
                            // 获取标签、内容、自定义内容
                            String title = xGNotifaction.getTitle();
                            String content = xGNotifaction.getContent();
                            String customContent = xGNotifaction
                                    .getCustomContent();
                            // 其它的处理
                            // 如果还要弹出通知，可直接调用以下代码或自己创建Notifaction，否则，本通知将不会弹出在通知栏中。
                            xGNotifaction.doNotify();
                        }
                    });

        }

    }

    public boolean isMainProcess() {
        ActivityManager am = ((ActivityManager) getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }
}
